package com.banshuibao.bsb_android.activity;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.XToast;

public class HelpDetailActivity extends BaseActivity implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener, View.OnClickListener, MediaPlayer.OnInfoListener {

    VideoView videoView;
    TextView title;
    ImageView play;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_detail);
        setTitleText("办税宝专家语音上传演示");
        initView();
        setData();
    }

    public void initView(){
        videoView = findViewById(R.id.videoView);
        title = findViewById(R.id.title);
        title.getPaint().setFakeBoldText(true);
        title.setText("办税宝专家语音上传演示");
        play = findViewById(R.id.play);
        play.setOnClickListener(this);
    }

    public void setData(){
        videoView.postDelayed(new Runnable() {
            @Override
            public void run() {
                videoView.setVisibility(View.VISIBLE);
            }
        },200);
        Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.showing);
        MediaController controller = new MediaController(this);
        controller.setMediaPlayer(videoView);
        videoView.setMediaController(controller);
        videoView.setOnCompletionListener(this);
        videoView.setOnErrorListener(this);
        videoView.setOnPreparedListener(this);
        videoView.setOnInfoListener(this);
        videoView.setVideoURI(uri);
        videoView.requestFocus();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        XToast.showToast(this,"播放完成");
        play.setVisibility(View.VISIBLE);
        videoView.requestFocus();
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        XToast.showToast(this,"获取视频错误");
        return false;
    }


    @Override
    public void onPrepared(MediaPlayer mp) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.play:
                play.setVisibility(View.GONE);
                videoView.start();
                break;
        }
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        if(mp!=null&&mp.isPlaying()){
            play.setVisibility(View.GONE);
        }
        return false;
    }
}
